package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.ConfigVacationBean;
import com.koron.css2.baseConfig.mapper.ConfigVacationMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 假期设置-编辑
 *
 * @author 廖大富
 */
public class ConfigVacationUpdate implements ServerInterface {

    private static Logger logger = LogManager.getLogger(ConfigVacationUpdate.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ConfigVacationMapper mapper = factory.getMapper(ConfigVacationMapper.class);
            ConfigVacationBean bean = JsonUtils.objectToPojo(req.getData(), ConfigVacationBean.class);
            // 校验字段重复
            if (mapper.check2("vacation_name", bean.getVacationName(), bean.getId()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "假期名称：" + bean.getVacationName() + "的信息已存在。", void.class);
            }
            if (StringUtils.isNullOrEmpty(bean.getId())) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "id为空", void.class);
            }
            if (StringUtils.isNullOrEmpty(bean.getVacationBeginDate()) || StringUtils.isNullOrEmpty(bean.getVacationEndDate())) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "假期开始日期和结束日期不能为空。", void.class);
            }
            bean.setUpdateInfo(userInfo);
            mapper.updateConfigVacation(bean);
            if (StringUtils.isNullOrEmpty(userInfo.getCurWaterCode())) {
                ConfigVacationList.synData(factory, bean);
            }
        } catch (Exception e) {
            logger.error("假期设置修改失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "假期设置修改失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }
}